package com.rookie.tool.module.util.clazz;


import groovy.lang.GroovyClassLoader;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.ConfigurableApplicationContext;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 动态生成类工具
 * @author rookie
 */
public class GenerateClassUtils {

    public static Class<?> generateClass(String classString){
        GroovyClassLoader groovyClassLoader = new GroovyClassLoader();
        return groovyClassLoader.parseClass(classString);
    }

    public static void main(String[] args) throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        String c = "package com.rookie.tool.module.util;\n" +
                "\n" +
                "public class Test {\n" +
                "   public void test(){\n" +
                "       System.out.println(666);\n" +
                "   }\n" +
                "}\n";
        Class<?> cType = generateClass(c);
        Object o = cType.newInstance();
        Method method = cType.getMethod("test");
        method.invoke(o);
    }
}
